package com.zx.controller;

import com.zx.pojo.SmbmsUser;
import com.zx.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller("usersc")
@RequestMapping("/users/")
public class UserController {
    //控制器层调用service层
    @Autowired
    private UsersService usersService;

    /**
     * @param userCode 前端页面的账户  name="userCode"
     * @param upwd     前端页面的密码   name="upwd"
     * @param session
     * @return
     */
    @RequestMapping("login")
    public String login(String userCode, String upwd, HttpSession session) {
        SmbmsUser user = usersService.login(userCode, upwd);
        if (user != null) {//成功
            session.removeAttribute("msg");
            session.setAttribute("logUser", user);
            // forward: 转发跳转到控制器
            return "forward:list";
        } else { //失败
            session.setAttribute("msg", "用户名或密码错误！");
            //重定向  到根目录 /login.jsp
            return "redirect:/login.jsp";
        }

    }

    @RequestMapping("list")
    public String list(Model model) {
        List<SmbmsUser> ulist = usersService.findAllByUsers();
        model.addAttribute("ulist", ulist);
        return "userlist";
    }

    //跳转到修改页面
    @RequestMapping("goupdate")
    public String goupdate(Long uid, Model model) {
        SmbmsUser smbmsUser = usersService.findUsersById(uid);
        model.addAttribute("euser", smbmsUser);
        return "usermodify";
    }

    @RequestMapping("doupdate")
    public String doupdate(SmbmsUser user, Model model) {
        boolean flag = usersService.updateUsers(user);
        if (flag) { //修改成功  再次查詢下
            return "forward:list";
        } else {  //修改失败
            model.addAttribute("msg", "修改失败！");
            return "usermodify";
        }

    }

    //跳转到修改页面
    @RequestMapping("details/{id}")
    public String details(@PathVariable("id") Long uid, Model model) {
        SmbmsUser smbmsUser = usersService.findUsersById(uid);
        model.addAttribute("duser", smbmsUser);
        return "userview";
    }

    //删除操作
    @RequestMapping("del")
    public String del(Long uid, Model model) {
        boolean flag = usersService.delUsers(uid);
        if (flag) {
            return "redirect: list";
        } else {
            model.addAttribute("msg", " 删除失败");
            return "userlist";
        }

    }

    //  /users/add
    @RequestMapping(name = "add", method = RequestMethod.GET)
    public String hello1(Model model) {
        System.out.println("进入了UserController----add()");
        model.addAttribute("msg", "响应内容add");
        return "success";
    }

    @RequestMapping("update")
    public String hello2(Model model) {
        System.out.println("进入了UserController----update()");
        model.addAttribute("msg", "响应内容update");
        return "success";
    }
}
